﻿function toggleSubject(elem)
{
    var subject = jQuery(elem).parent().parent();
    if(subject.hasClass('opened'))
    {
        subject.find("ul").hide();
        subject.removeClass('opened').addClass('closed'); 
    }
    else 
    {        
        subject.find('ul').show();
        subject.removeClass('closed').addClass('opened'); 
    }
}

function stringToBool(boolStr) {
    switch (boolStr.toLowerCase()) 
    {
        case "true": case "True": case "yes": case "1": return true;
        case "false": case "Fasle": case "no": case "0": case null: return false;
        default: return Boolean(boolStr);
    }
}

function redirectTo(target)
{
    var urlParts = window.location.toString().split('/');
    var newUrl = '';
    for(i = 0; i < urlParts.length - 1; i++)
    {
        newUrl += urlParts[i];
        newUrl += "/";
    }
    newUrl += target;
    window.location = newUrl;
}

function testRun(questionPanelId, allowReAnswer) 
{
    allowReAnswer = stringToBool(allowReAnswer);   
    var textAnswer = "",
        answers = [],
        curTest = $("#test-sidebar ul li:first").addClass("current"),
        answered = "",
        panelId = questionPanelId,
        loadingImg = $("div.testrunner p.buttons img");

    var testRunner = {

        submitTest: function() {
            answered = "";
            textAnswer = $("#" + panelId + " input:text").val() || null;
            var tempAns = [];
            $("#" + panelId + " input:radio:checked").each(function() {
                tempAns.push(this.value);
            }
            );
            $("#" + panelId + " input:checkbox:checked").each(function() {
                tempAns.push(this.value);
            }
            );
            answers = tempAns;
            var isFinish = true;
            if (curTest.next().get().length == 0 && allowReAnswer)
                isFinish = confirm("Ви впевнені що хочете закінчити тест ?");
            if (isFinish) {
                loadingImg.css({ display: "" });
                PageMethods.SubmitTest($("#test-sidebar").html(), textAnswer, answers,
                                       testRunner.onSubmitTestSuccess, testRunner.onSubmitTestFail);
                curTest.addClass("answered").removeClass("current");
                curTest = curTest.next();
            }
            return false;
        },

        getTest: function(elem) {
            curTest.removeClass("current");
            curTest = elem == null ? curTest : $(elem.currentTarget);
            curTest.addClass("current");
            PageMethods.GetTest(curTest.children().children().text(),
                                testRunner.onGetTestSuccess, testRunner.onGetTestFail);
        },

        getNextTest: function() {
            PageMethods.GetNextTest(testRunner.onGetTestSuccess, testRunner.onGetTestFail);
        },

        onSubmitTestSuccess: function(result, context, methodName) {
            if (curTest.get().length == 0 && result == "0") {
                redirectTo("QuizResult.aspx");
            }
            if(allowReAnswer)
                testRunner.getTest(null);
            else
                testRunner.getNextTest();
        },

        onSubmitTestFail: function(error, context, methodName) {

        },

        onGetTestSuccess: function(result, context, methodName) {
            $("#" + panelId).html(result);
            loadingImg.css({ display: "none" });
        },

        onGetTestFail: function(error, context, methodName) {
        }
    };
    return testRunner;
}

    

